Compilers typically convert source code into object code or some other intermediate code representation, such as byte codes. Source code is a set of instructions written in a programming language by a software developer. Object code, in general, is machine code or other low level code, translated from source code toward execution by a system""s central processing unit (CPU). Compilation of source code can occur either statically or dynamically.
Statically compiled programs are compiled once and the resulting binary object code is used repeatedly. Typically, static compilation occurs prior to shipping the program to the end user. Therefore, the time required to compile a program in static compilation environments does not effect the performance of the program when executed by the end user.
Dynamic compilation execution environments, such as web browsers, involve the compilation of source code simultaneously or time-sharing with the execution of a program. Dynamic compilation is advantageous because code is shipped in a platform independent form and the compilation can use information about the target machine and security environment. However, the time required to dynamically compile the source code directly impacts the speed of the program execution.
Programming and scripting languages provide the software developer with a set of features and tools for organizing source code within a program. Class templates and classes generated from class templates (hereinafter template-generated classes) are examples of tools provided by some programming and scripting languages within static compilation environments.
In object-oriented programming, a class is a descriptive tool used in source code for characterizing an object as a set of data structures (i.e. fields) or a set of routines (i.e. methods). For example, an array of integers can be represented in a class, IntegerArray. Such a class may include a field for storing the size of the array and another field comprising a data structure for representing the integers stored in an IntegerArray. In addition, the IntegerArray class may include methods for accessing the contents of the array, such as xe2x80x9cgetxe2x80x9d and xe2x80x9csetxe2x80x9d methods.
Once compiled, the class is represented as an object defining storage allocation for the class fields and specifying the types of data that can be stored within them. In addition, the class object provides access to executable object code for executing class methods. Instances of a class are generated from this class representation.
An instance is an object that exists during program execution that functions in accordance with the guidelines specified by the class representation. An instance stores its own values in the storage allocated for the class fields and executes methods compiled and stored by the class representation. An instance is created when a class is instantiated during program execution. Instantiation is the process of creating an instance of a class, typically, by calling a special operator or function (e.g., xe2x80x9cnew { }xe2x80x9d).
Class templates are useful programming tools for generating classes that provide the same basic functionality, but differ with respect to the types of data that are stored and serviced within the class. FIG. 1 is a block diagram illustrating an object hierarchy involving class templates. A class template 10 generates class 30 which is used to declare and instantiate class instances 50a and 50b. Likewise, class template 10 generates class 35 which is used to declare and instantiate class instances 55a and 55b. 
A class template has source code forming the foundation for new template-generated classes. Different template-generated classes can be generated from the same class template definition by specifying different parameters. A parameter can be a primitive data type, class, value, or other data structure. Therefore, the use of class templates facilitates the reuse of source code.
The use of class templates also leads to more robust software, because such template-generated classes can be type-checked when compiled. In addition, the use of class templates in generating classes leads to faster software, because the compiler can generate efficient code with the knowledge of the parameters defining such classes.
Using static compilation techniques for dynamically compiling a software program would be slow and inefficient. For example, compiling an entire program may be wasteful of both time and memory when some portions of a program, such as an error handling routine, may not be used.
With respect to template-generated classes, the unnecessary object code results, is in part, from compiling source code that defines the body of class methods that are never invoked during program execution. In addition, unnecessary object code results from the generation of method bindings for class methods that are never referenced in the program code. A method binding is an object that stores information about an individual class method and is used to compile instructions in the program code that reference that method. Program code is the source code for the program in general.
The excess object code results in the program requiring more space on disk and, possibly, more memory (i.e. RAM) during program execution. When a program is statically compiled, the extra compilation time is tolerated because performance, in general, is not effected during program execution. However, in dynamic compilation execution environments, the program code is compiled at execution time so that extra compilation time makes the program run more slowly. Therefore, static compilation techniques implemented to compile template-generated classes would be prohibitive in both memory use and program performance.
The Java programming language is used, in part, to develop applets (i.e. programs that only execute within dynamic compilation execution environments, such as a web browser), but does not support class templates in its current language specification. Instead a Java programmer can use the class, Object, to store different data types, values, or structures. However, a disadvantage of using the Object class is that a Java compiler can not perform type-checking on Object-based classes until the program is running. This makes the software less robust, because any type errors are detected only at runtime by a runtime type dispatch.
There is a current proposal to add class templates to the Java programming language. However, the current proposal restricts the usage of parameters within the source code definition of a class template, limiting some of its benefits.
Therefore, it would be useful to be able to compile template-generated classes obtaining the full benefits of class templates without the issues of excess code generation and compilation time in dynamic compilation execution environments.
The following description discloses embodiments of the invention providing lazy compilation of template-generated classes in dynamic compilation execution environments. One embodiment involves creating a representation of the class template, creating a representation of a class generated from the class template, and generating method bindings, stored in the class representation, for only those methods referenced in the program code. This process of generating method bindings for referenced methods decreases the amount of code generated in compiling such classes and decreases the amount of time required to perform the compilation.
In addition, lazy compilation may include delaying compilation of the body of a referenced method until the class method is invoked by the execution of a method call instruction. Embodiments of the invention which provide for the compilation of a method body include execution of stub code initially referenced through an indexed method table for the invoked method. This action occurs in response to the class method being invoked to an instance of a template-generated class. The stub code initiates the compilation of the body of the invoked method resulting in executable object code. The address of the executable code replaces the address of the stub code in the method table for future invocation of the method by other instances of the same class.
Furthermore, embodiments of the invention which decrease the time required to compile the a method body include a process of code sharing. Code sharing includes sharing executable object code for compatible methods among different classes generated from the same class template. Executable object code is stored or referenced by a cache of the class template representation. A determination is made as to the compatibility of the method to be compiled and the cached object code. If compatible, the compilation of the method is avoided by simply replacing the address of the stub code with the address of the cached code.
In general, full compilation of methods for a particular class is performed for only those methods that are referenced in the program code, invoked during program execution, and are incompatible with previously compiled methods.
Thus, certain aspects of the invention involve compiling program code which includes a class template and a template-generated class in a dynamic compilation execution environment. Such compilation involves (1) creating a representation of the class template from its definition within the program code; (2) creating a representation of the template-generated class from its declaration within the program code, such that it points to the class template representation; and (3) generating method bindings stored in the class representation for methods referenced within the program code. The method bindings generated are substantially limited to those methods referenced in the program code. In addition, the compilation may involve (4) delaying compilation of method bodies for those methods referenced in the program code until they are invoked.
Each method within a class template is represented by a method descriptor storing or referencing the source code representations of a type signature and method body for that method. The source code representation of the type signature is used in the process of generating a method binding for the template-generated class representation.
The process for generating method bindings is triggered whenever a method call instruction, referencing a class method of a template-generated class, requires compilation. It involves (1) scanning the class representation for a previously generated method binding representing the referenced method; and if not found, (2) creating a method binding; (3) scanning the class template representation for the source code representation of the method""s type signature; (4) compiling the source code representation of the type signature; (5) associating the resulting type signature object and method offset with the method binding; and (6) associating the method binding with the class representation. Once, the method binding is found or generated, the method call instruction is compiled using the type signature object and method offset.
The compiling of method bodies involves a class template representation which includes a method table mapping the methods of the template to stub code. The method table is referenced by the class template representation through a method table array. A copy of the method table is referenced by the class representation initially providing access to stub code for each method invoked on an instance of a template-generated class.
When a class method is invoked on an instance of a class representation during program execution. The method table of the class representation is referenced for the address of the stub code associated with the invoked method. The stub code is executed initiating the compilation of the source code representation of a method body such that a runtime compiler is invoked. The runtime compiler is invoked with the address of the source code representation of the method body stored within the class template representation. The address of the resulting executable code subsequently replaces the address of the stub code in the method table of the class representation.
Alternatively, the compilation of method bodies includes (1) referencing a cache of the class template representation for cached executable code for the invoked method; (2) determining whether the cached executable code is compatible with the invoked method; and if compatible, (3) replacing the address of the stub code in the method table with the address of the cached executable code.
A cache may be stored within a method descriptor of the class template representation, such that the cache includes the address of cached executable code or the address of a table mapping rules to cached executable code. The rules of the table provide criteria for determining whether the cached executable code is compatible with the invoked method.